// Object -> Reflect
// let obj = {}
// let newVal = ''
// Reflect.defineProperty(obj, 'name', {
//   get(){
//     return newVal
//   },
//   set(val){
//     newVal = val
//   }
// })
// obj.name = 'es'
// console.log(obj.name);

// try {
//   Object.defineProperty()
// } catch(e){  
//   console.log(e.message);
// }
// if(Reflect.defineProperty()){ // true
//   console.log('Reflect Boolean');
// }

// console.log('assign' in Object);
// console.log(Reflect.has(Object,'assign'));


let user = {
  name: 'xiecheng',
  age: 34,
  _password: '***'
}
user = new Proxy(user, {
  get(target, prop) {
    if (prop.startsWith('_')) {
      throw new Error('不可访问')
    } else {
      // return target[prop]
      return Reflect.get(target,prop)
    }
  },
  set(target, prop, val) {
    if (prop.startsWith('_')) {
      throw new Error('不可写入')
    } else {
      // target[prop] = val
      Reflect.set(target,prop,val)
      return true
    }
  },
  deleteProperty(target, prop) {
    if (prop.startsWith('_')) {
      throw new Error('不可删除')
    } else {
      // delete target[prop]
      Reflect.deleteProperty(target,prop)
      return true
    }
  },
  ownKeys(target) {
    // return Object.keys(target).filter(key => !key.startsWith('_'))
    return Reflect.ownKeys(target).filter(key => !key.startsWith('_'))
  }
})
try {
  console.log(user._password);
} catch (e) {
  console.log(e.message);
}
try {
  user._password = 'xxx'
} catch (e) {
  console.log(e.message);
}

try {
  delete user._password
} catch (e) {
  console.log(e.message);
}

try {
  console.log(Object.keys(user));
} catch (e) {
  console.log(e.message);
}


